package com.yyq.sos.permission;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.yyq.adms.usermanage.bpo.SysUsersBpo;
import com.yyq.servlets.ApplicationContextHandle;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.constants.Constants;


public class Permissions
{
	private Map map = new HashMap();
	public static Map rightMap = new HashMap();
	public List rightList=new ArrayList();
	private SysUsersBpo sysUserBpo;
	
	static{
		rightMap.put(ActionType.ACTION_DATA_LIST,Constants.RIGHT_READ);
		rightMap.put(ActionType.ACTION_CHILD,Constants.RIGHT_READ);
		rightMap.put(ActionType.ACTION_GO_TREE,Constants.RIGHT_READ);
		rightMap.put(ActionType.ACTION_LIST,Constants.RIGHT_READ);
		rightMap.put(ActionType.ACTION_MAIN,Constants.RIGHT_READ);
		rightMap.put(ActionType.ACTION_MENU,Constants.RIGHT_READ);
		rightMap.put(ActionType.ACTION_OTHRE,Constants.RIGHT_READ);
		rightMap.put(ActionType.ACTION_TREE,Constants.RIGHT_READ);
		rightMap.put(ActionType.ACTION_SEARCH,Constants.RIGHT_READ);
		rightMap.put(ActionType.ACTION_DOWNLOAD,Constants.RIGHT_READ);
		rightMap.put(ActionType.ACTION_MSG,Constants.RIGHT_READ);
		rightMap.put(ActionType.ACTION_MSG_NOTIFY,Constants.RIGHT_READ);
		
		rightMap.put(ActionType.ACTION_ADD,Constants.RIGHT_WRITE);
		rightMap.put(ActionType.ACTION_GO_ADD,Constants.RIGHT_WRITE);
		rightMap.put(ActionType.ACTION_UPLOAD,Constants.RIGHT_WRITE);
		rightMap.put(ActionType.ACTION_DELETE,Constants.RIGHT_DELETE);
		rightMap.put(ActionType.ACTION_GO_UPDATE,Constants.RIGHT_UPDATE);
		rightMap.put(ActionType.ACTION_UPDATE,Constants.RIGHT_UPDATE);
		
		rightMap.put(ActionType.ACTION_VIEW,Constants.RIGHT_VIEW);
		rightMap.put(ActionType.ACTION_GO_VIEW,Constants.RIGHT_VIEW);
		
		rightMap.put(ActionType.ACTION_ALL_SEARCH,Constants.RIGHT_SEARCH);
		rightMap.put(ActionType.ACTION_GO_All_SEARCH,Constants.RIGHT_SEARCH);
		
		rightMap.put(ActionType.ACTION_PRINT,Constants.RIGHT_PRINT);
		rightMap.put(ActionType.ACTION_PREVIEW,Constants.RIGHT_PREVIEW);
		
		rightMap.put(ActionType.ACTION_EXPORT,Constants.RIGHT_EXPORT);
		rightMap.put(ActionType.ACTION_EXPORT_ALL,Constants.RIGHT_EXPORT);
		
		rightMap.put(ActionType.ACTION_GO_CHECK,Constants.RIGHT_CHECK);
		rightMap.put(ActionType.ACTION_CHECK,Constants.RIGHT_CHECK);
		
		rightMap.put(ActionType.ACTION_GO_BATCH_CHECK, Constants.RIGHT_CHECK);
		rightMap.put(ActionType.ACTION_BATCH_CHECK, Constants.RIGHT_CHECK);
		rightMap.put(ActionType.ACTION_LOOK_CHECK_DATA, Constants.RIGHT_CHECK);
		rightMap.put(ActionType.ACTION_LOOK_CHECK_INFO, Constants.RIGHT_CHECK);
		
		rightMap.put(ActionType.ACTION_GO_BATCH_UPDATE_PERIOD, Constants.RIGHT_UPDATE);
		rightMap.put(ActionType.ACTION_BATCH_UPDATE_PERIOD, Constants.RIGHT_UPDATE);
		
		rightMap.put(ActionType.ACTION_BATCH_ADD, Constants.RIGHT_WRITE);
		rightMap.put(ActionType.ACTION_GO_BATCH_ADD,Constants.RIGHT_WRITE);
		rightMap.put(ActionType.ACTION_BATCH_DATA_LIST,Constants.RIGHT_WRITE);
		rightMap.put(ActionType.ACTION_BATCH_SETDATE,Constants.RIGHT_WRITE);
		rightMap.put(ActionType.ACTION_BATCH_AD_DESC,Constants.RIGHT_WRITE);
		rightMap.put(ActionType.ACTION_BATCH_REMARK,Constants.RIGHT_WRITE);
		rightMap.put(ActionType.ACTION_CHANGE, Constants.RIGHT_WRITE);
		//
		rightMap.put(ActionType.ACTION_COPY,Constants.RIGHT_WRITE);
		
		rightMap.put(ActionType.ACTION_CONDITION,Constants.RIGHT_UPDATE);
	}
	
	public Permissions(LoginUser user)
	{
		if(sysUserBpo==null){
			sysUserBpo = (SysUsersBpo)ApplicationContextHandle.getBean("sysUsersBpo");
		}
		List list = sysUserBpo.getModulePermByUser(user);
		List mcs = new ArrayList();
		for (int i = 0; i < list.size(); i++) 
		{
			Object[] mp = (Object[]) list.get(i);
			String moduleId = mp[0].toString().trim();
			if (mcs.contains(moduleId))
				continue;
			mcs.add(moduleId);
			List permNames = new ArrayList();
			for (int j = 0; j < list.size(); j++)
			{
				Object[] obj = (Object[]) list.get(j);
				if (moduleId.equals(obj[0].toString().trim())) {
					permNames.add(obj[1]);
				} 
			}
			map.put(moduleId, permNames);
		}
		
	}
	
	public boolean hasRight(String moduleId,String cmd)
	{
		String right = (String)rightMap.get(cmd);
		List list = null;
		if(right==null)
			return false;
		list = (List)map.get(moduleId);
		System.out.println("moduleId="+moduleId);
		System.out.println("Ȩ�޼��ϣ�������"+list);
		//rightList:cmd���ϣ�ÿ�η���ʱ��Ҫ���
		this.rightList.clear();
		for(Object o:rightMap.keySet()){
			if(list!=null&&list.size()!=0){
				for(Object o2:list){
					if(rightMap.get(o).equals(o2.toString())&&!this.rightList.contains(o)){
						this.rightList.add(o.toString());
					}
				}
			}
		}
		System.out.println("-----------------------------------");
		System.out.println("cmdList==="+rightList);
		System.out.println("-----------------------------------");
		System.out.println("list==="+list);
		if(list==null)
			return false;
		return list.contains(right);
	}
	
	public boolean hasOptRight(String moduleId, String right){
		List list = (List)map.get(moduleId);
		if(list==null)
			return false;
		return list.contains(right);
	}

	public SysUsersBpo getSysUserBpo() {
		return sysUserBpo;
	}

	@Resource(name="sysUsersBpo")
	public void setSysUserBpo(SysUsersBpo sysUserBpo) {
		this.sysUserBpo = sysUserBpo;
	}
}
